/**
 * 
 */
package com.mvc.controller.skdata;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.huangqj.consant.GlobalConstant;
import org.huangqj.consant.ValidatorUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mvc.model.vo.BloodGlu;
import com.mvc.service.vo.BloodGluServiceI;
import com.mvc.utils.HttpString;

/**
 * @author qingjie
 *
 */
@Controller
@RequestMapping("/http")
public class HttpController {
	
	@Autowired
	private BloodGluServiceI bgs;
	
	private boolean b = false;//是否返回值
	private boolean bl = false;//是否处理该条数据
	private Date date = new Date(System.currentTimeMillis());
	private String nowTime = GlobalConstant.sdf_all.format(date);
	private String returnValue = null;
	@RequestMapping("/bloodglu")
	@ResponseBody
	public String getBloodGlu(BloodGlu bg,HttpServletRequest request){
		if (bg!=null) {
			//如果服务器接收默认时间第一位为0时，那么就为默认时间
			if(HttpString.isNowTime(bg.getTestTime())){
				bg.setTestTime(nowTime);
				b = true;//做正常返回
			}
			
			//如果测试时间大于现在时间则重新设置为系统现在时间
			if(!b&&HttpString.timeSubtract(nowTime, bg.getTestTime())){
				bg.setTestTime(nowTime);
				b = true;//做正常返回
			}
			
			//如果血糖数值在0.8~34mmol/l之外数据不做处理（删除）,但要有返回值
			if(!HttpString.isAccordBloodGlu(bg.getBloodGlu())){
				b = true;//做正常返回
				bl = false;//不处理该条数据
			}else b = true;
			
			//重复数据（设备id+采血时间+血糖值）相同为重复，则否
			if(bgs.isRepeat(bg.getDevid(), bg.getTestTime(), bg.getBloodGlu())){
				bl = false;
			}
			//处理该条数据，进行持久化
			if(bl){
				String id = bgs.saveBloodGlu(bg);
				if(!ValidatorUtil.isBlankOnString(id)){
					b = true;
				}else b = false;
			}
		}
		
		//符合条件，重新组装返回值
		if(b){
			returnValue = "OK"+nowTime;
		}
		return returnValue;
	}

}
